/*
 * Copyright 2012 JooKar BV (info@jookar.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.jookar.android.simpleclock.views;

import java.util.Date;

import nl.jookar.android.simpleclock.R;
import nl.jookar.android.simpleclock.SimpleClockApp;
import nl.jookar.android.utils.misc.SystemUtils;
import android.content.Context;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;

/**
 * @author Joost A. Bloemsma
 *
 */
public class AnimatedDigitView extends RelativeLayout implements IClockView {
	private static final String DEBUG_TAG = AnimatedDigitView.class.getName();
	private SingleAnimatedDigit[] digits = new SingleAnimatedDigit[8];
	private String format;
	
	/**
	 * Constructs a new AnimatedDigitView instance
	 */
	public AnimatedDigitView() {
		this(SimpleClockApp.getAppContext());
	}
	
	/**
	 * Constructs a new AnimatedDigitView instance
	 * @param context
	 */
	public AnimatedDigitView(Context context) {
		super(context);
		init();
	}

	/**
	 * Constructs a new AnimatedDigitView instance
	 * @param context
	 * @param attrs
	 */
	public AnimatedDigitView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
		Log.d(DEBUG_TAG, "Initialising AnimatedDigitView");
		create();
		getControls();
		this.setSwapAnimation(new HorizontalRollAnimation());
	}
	
	private void create() {
		LayoutInflater layoutInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.animateddigitsview, this, true);
	}
	
	private void getControls() {
		digits[0] = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit0);
		digits[1] = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit1);
		digits[2] = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit2);
		digits[3] = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit3);
		digits[4] = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit4);
		digits[5] = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit5);
		digits[6] = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit6);
		digits[7] = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit7);
	}

	/**
	 * @param swapper
	 */
	public void setSwapAnimation(IAnimatedViewSwapper swapper) {
		// Only set the swapper for one digit, since it is static.
		digits[0].setSwapAnimation(swapper);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setTime(Date date) {
		String time = (String)DateFormat.format(this.format, date);
		char[] ch = time.toCharArray();

		for (int i = 0; i < 8; i++) {
			digits[i].setChar(ch[i]);
		}
		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void set24H(boolean fmt24) {
		if (fmt24) {
			format = SimpleClockApp.getAppContext().getString(R.string.fmt_24h);
			Log.d(DEBUG_TAG, "Format set to 24h");
		} else {
			format = SimpleClockApp.getAppContext().getString(R.string.fmt_12h);
			Log.d(DEBUG_TAG, "Format set to 12h");
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean addClockMenu(Menu menu) {
		for (ClockType type : ClockType.values()) {
	    	menu.add(Menu.NONE , type.getId(), Menu.NONE, type.getName());
	    }
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		for (ClockType type : ClockType.values()) {
			if (item.getItemId() == type.getId()) {
				this.setSwapAnimation(SystemUtils.createInstance(type.getSwapperClass()));
				return true;
			}
		}
		
		return false;
	}
	
	
}
